package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加菜品和套餐+
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Category category) {
        log.info("category:{}", category);
        categoryService.save(category);
        return R.success("添加成功!");
    }


    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> list(@RequestParam int page, int pageSize) {
        Page p = new Page(page, pageSize);
        LambdaQueryWrapper<Category> lam = new LambdaQueryWrapper<>();
        lam.orderByAsc(Category::getSort);
        categoryService.page(p, lam);
        return R.success(p);
    }

    /**
     * 分类修改
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 分类删除
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam("ids") Long id) {
        log.info("删除分类，id为：{}", id);
        categoryService.delete(id);
        return R.success("删除成功!");
    }

    @GetMapping("/list")
    public R<List<Category>> ll(Category category){

        LambdaQueryWrapper<Category> lam = new LambdaQueryWrapper<>();
        lam.eq(category.getType()!=null,Category::getType,category.getType());
        List<Category> list = categoryService.list(lam);
        return R.success(list);
    }


}
